function es = end(A,K,N)
% transio::end  - overloaded method
%
% FORMAT:       ES = end(obj, K, N)
%
% Input fields:
%
%       obj         1x1 transio object
%       K           end-dimension
%       N           number of total dimensions in expression
%
% Output fields:
%
%       ES          end (size) of (remaining) dimension(s)

% Version:  v0.7g
% Build:    9101517
% Date:     Oct-15 2009, 5:08 PM CEST
% Author:   Jochen Weber, SCAN Unit, Columbia University, NYC, NY, USA
% URL/Info: http://wiki.brainvoyager.com/BVQXtools

% check arguments
if nargin < 3 || ...
   ~isa(A, 'transio') || ...
   ~isa(N, 'double') || ...
   ~isa(K, 'double') || ...
    numel(N) ~= 1 || ...
    numel(K) ~= 1 || ...
    isinf(K) || ...
    isnan(K) || ...
    K < 1 || ...
    N < K
    error( ...
        'transio:BadArgument', ...
        'Bad or too few arguments for transio::end.' ...
    );
end
K = floor(K);

% get size
csz = A.DataDims;
csl = numel(csz);

% if end requested beyond defined size
if K > csl
    
    % return 1
    es = 1;
    
% otherwise
else
    
    % make sure N is valid
    N = max(1, ceil(N));
    
    % collapse if N < number of desired dims
    if N < csl && ...
        K == N
        csz(N) = prod(csz(N:end));
    end

    es = csz(K);
end
